package com.cskaoyan.market.admin.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.db.vo.MarketCategoryL1Vo;
import com.cskaoyan.market.db.vo.MarketCategoryVo;
import com.cskaoyan.market.admin.service.MarketCategoryService;
import com.cskaoyan.market.util.CategoryLevelConstant;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @NAME: MarketCategoryServiceImpl
 * @DESCRIPTION: 商品类目持久层
 * @DATE: 2023/5/8 23:16
 * @AUTHOR: 刘超
 */
@Service
public class MarketCategoryServiceImpl implements MarketCategoryService {
    @Autowired
    private MarketCategoryMapper mapper;

    @Override
    public List<MarketCategoryVo> list() {
        
        
        MarketCategoryExample example = new MarketCategoryExample();
        example.orderBy("id asc").or().andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = mapper.selectByExample(example);;
        
        

        ArrayList<MarketCategory> marketCategoriesL1 = new ArrayList<>();
        ArrayList<MarketCategory> marketCategoriesL2 = new ArrayList<>();

        Map<String, List<MarketCategory>> map = new HashMap<>();
        map.put(CategoryLevelConstant.LEVEL_1, marketCategoriesL1);
        map.put(CategoryLevelConstant.LEVEL_2, marketCategoriesL2);
        for (MarketCategory marketCategory : marketCategories) {
            // 两个条件判断的作用，按照等级划分到两个块里
            if (marketCategory.getLevel().equals(CategoryLevelConstant.LEVEL_1)) {
                marketCategoriesL1.add(marketCategory);
            }
            if (marketCategory.getLevel().equals(CategoryLevelConstant.LEVEL_2)) {
                marketCategoriesL2.add(marketCategory);
            }
        }
        List<MarketCategoryVo> l1CategoryVos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategoriesL1) {
            MarketCategoryVo marketCategoryVoL1 = new MarketCategoryVo();
            marketCategoryVoL1.setId(marketCategory.getId());
            marketCategoryVoL1.setName(marketCategory.getName());
            marketCategoryVoL1.setKeywords(marketCategory.getKeywords());
            marketCategoryVoL1.setIconUrl(marketCategory.getIconUrl());
            marketCategoryVoL1.setPicUrl(marketCategory.getPicUrl());
            marketCategoryVoL1.setLevel(marketCategory.getLevel());
            List<MarketCategoryVo> l2CategoryVos = new ArrayList<>();
            for (MarketCategory category : marketCategoriesL2) {
                if (marketCategory.getId().equals(category.getPid())) {
                    MarketCategoryVo marketCategoryVoL2 = new MarketCategoryVo();
                    marketCategoryVoL2.setId(category.getId());
                    marketCategoryVoL2.setName(category.getName());
                    marketCategoryVoL2.setKeywords(category.getKeywords());
                    marketCategoryVoL2.setIconUrl(category.getIconUrl());
                    marketCategoryVoL2.setPicUrl(category.getPicUrl());
                    marketCategoryVoL2.setLevel(category.getLevel());
                    l2CategoryVos.add(marketCategoryVoL2);
                }
            }
            marketCategoryVoL1.setChildren(l2CategoryVos);
            l1CategoryVos.add(marketCategoryVoL1);
        }
        return l1CategoryVos;
    }

    @Override
    public List<MarketCategoryL1Vo> l1() {
        
        
        MarketCategoryExample example = new MarketCategoryExample();
        example.orderBy("id asc").or().andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = mapper.selectByExample(example);;
        
        
        List<MarketCategoryL1Vo> marketCategoryL1Vos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            MarketCategoryL1Vo categoryL1Vo = new MarketCategoryL1Vo();
            if (marketCategory.getLevel().equals(CategoryLevelConstant.LEVEL_1)){
                categoryL1Vo.setLabel(marketCategory.getName());
                categoryL1Vo.setValue(marketCategory.getId());
                marketCategoryL1Vos.add(categoryL1Vo);
            }
        }
        return marketCategoryL1Vos;
    }


    /**
     * 已弃用的查询
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @Override
    public List<MarketCategory> list(String page, String limit, String sort, String order) {
        // 获取session
        
        
        //  构建查询条件
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.orderBy(sort + " " + order).or().andDeletedEqualTo(false);
        // 分页查询
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCategory> marketCategories = mapper.selectByExample(marketCategoryExample);
        
        
        return marketCategories;
    }

    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        
        
        marketCategory.setDeleted(false);
        mapper.insert(marketCategory);

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        
        categoryExample.or().andNameEqualTo(marketCategory.getName());
        return mapper.selectOneByExample(categoryExample);
    }

    @Override
    public MarketCategory update(MarketCategory marketCategory) {
        
        
        mapper.updateByPrimaryKey(marketCategory);
        mapper.selectByPrimaryKey(marketCategory.getId());

        
        
        return marketCategory;
    }

    @Override
    public int delete(MarketCategory marketCategory) {
        
        
        mapper.logicalDeleteByPrimaryKey(marketCategory.getId());
        
        
        return 0;
    }


}
